﻿using Koala.Pro.Common.Extension;
using Koala.Pro.Core;
using System.Security.Claims;
using System.Security.Principal;

namespace Koala.Pro.Models
{
    [Serializable]
    public class ApiUserPrincipal : IApiUserSession
    {
        private bool m_ThreadPolicySet;

        protected ClaimsPrincipal Principal => (Thread.CurrentPrincipal as ClaimsPrincipal);

        public string Channel => Principal?.FindFirst("Channel")?.Value;

        public string IP => Principal?.FindFirst("IP")?.Value;

        public string MacAddr => Principal?.FindFirst("MacAddr")?.Value;

        public int? Id => Principal?.FindFirst("id")?.Value.ToInt32();

        public string Name => Principal?.FindFirst("name")?.Value;

        public string Email => Principal?.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value;

        public string Mobile => Principal?.FindFirst("phone_number")?.Value;

        public string Gender => Principal?.FindFirst("gender")?.Value;

        public string FullName => Principal?.FindFirst("FullName")?.Value;

        public string Company_ID => Principal?.FindFirst("Company_ID")?.Value;

        public string CompanyName => Principal?.FindFirst("CompanyName")?.Value;

        public string Dept_ID => Principal?.FindFirst("Dept_ID")?.Value;

        public string DeptName => Principal?.FindFirst("DeptName")?.Value;

        public string RoleType => Principal?.FindFirst("RoleType")?.Value;

        public ApiUserPrincipal()
        {
        }

        public void SetInfo(LoginUserInfo info)
        {
            ClaimsIdentity claimsIdentity = new ClaimsIdentity("login");
            claimsIdentity.AddClaim(new Claim("id", info.Id ?? ""));
            claimsIdentity.AddClaim(new Claim("name", info.Name ?? ""));
            claimsIdentity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", info.Email ?? ""));
            claimsIdentity.AddClaim(new Claim("phone_number", info.MobilePhone ?? ""));
            claimsIdentity.AddClaim(new Claim("Gender", info.Gender ?? ""));
            claimsIdentity.AddClaim(new Claim("FullName", info.FullName ?? ""));
            claimsIdentity.AddClaim(new Claim("Company_ID", info.CompanyId ?? ""));
            claimsIdentity.AddClaim(new Claim("CompanyName", info.CompanyName ?? ""));
            claimsIdentity.AddClaim(new Claim("Dept_ID", info.DeptId ?? ""));
            claimsIdentity.AddClaim(new Claim("DeptName", info.DeptName ?? ""));
            claimsIdentity.AddClaim(new Claim("Channel", info.Channel ?? ""));
            claimsIdentity.AddClaim(new Claim("IP", info.IP ?? ""));
            claimsIdentity.AddClaim(new Claim("MacAddr", info.MacAddr ?? ""));
            claimsIdentity.AddClaim(new Claim("RoleType", info.RoleType ?? ""));
            IPrincipal principal = new GenericPrincipal(claimsIdentity, null);
            if (!m_ThreadPolicySet)
            {
                m_ThreadPolicySet = true;
                AppDomain.CurrentDomain.SetThreadPrincipal(principal);
            }
            Thread.CurrentPrincipal = principal;
        }
    }
}
